﻿Option Explicit On
Option Strict On

Partial Class private_secure_members_pages_Dashboard
    Inherits System.Web.UI.Page
    Dim u As MembershipUser
    Dim PersonId As Int32
    Dim delid As Integer
    Dim EntityId As Integer

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'count of Entities
        'Your First Name, your role, registered since date
        'Your account status, payment details, Usage Plan
        'Your Usage Plan
        'Count of Entities
        'Count of Associates
        'Count of Single-use Login tokens applied for
        'Count of single-use login tokens used
        'Count of Associate Requests Made
        'Count of Associate Requests not Accepted
        'How many Campaigns created
        'How many active campaigns
        'How many email lists
        'How many text lists
        'How many email recipients
        'How many text recipients
        'Total Messages Sent
        'How many email messages sent?
        'How many Text Messages Sent
        u = Membership.GetUser(HttpContext.Current.User.Identity.Name)
        ' deals with session timeouts to reduce errors to the user
        If u Is Nothing Then
            FormsAuthentication.SignOut()
            Response.Redirect("~/Account/Login.aspx")
        End If

        If Not Page.IsPostBack Then

            ' Changed this around
            'If an associate fine
            If User.IsInRole("ListAssociate") Then
                ddlEntity.DataTextField = "Name"
                ddlEntity.DataValueField = "ID"
                ddlEntity.DataSource = MyEntityODS0
                ddlEntity.DataBind()
                ddlEntity.Items.Insert(0, New ListItem("Select item from list", [String].Empty))
                ddlEntity.SelectedIndex = 0
            Else
                ' If User.IsInRole("ListAgent") Then
                ddlEntity.DataTextField = "Name"
                ddlEntity.DataValueField = "ID"
                ddlEntity.DataSource = MyEntityODS
                ddlEntity.DataBind()
                ddlEntity.Items.Insert(0, New ListItem("Select item from list", [String].Empty))
                ddlEntity.SelectedIndex = 0
                ' End If
            End If

            'Set the initial views
            'Me.MultiViewCampaign.SetActiveView(Me.ViewCampaign_Adhoc)
            Me.MultiViewContact.SetActiveView(Me.ViewContact_Mobile)
            Me.MultiViewContent.SetActiveView(Me.ViewContent_Adhoc)
            Me.MultiViewFilter.SetActiveView(Me.ViewFilter_Unfiltered)

        End If
        Try
            Me.ddlEntity.DataBind()

        Catch ex As Exception

        End Try
        'FormView1.DataBind()
    End Sub
    Protected Sub MyCampaignODS_Selecting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles MyEntityODS.Selecting
        e.InputParameters("UserId") = u.ProviderUserKey
    End Sub
    Protected Sub MyCampaignODS0_Selecting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles MyEntityODS0.Selecting
        e.InputParameters("UserId") = u.ProviderUserKey
    End Sub

    Protected Sub ddlTargetList_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlTargetList.SelectedIndexChanged
        Me.lblRecipientName.Text = ""
    End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnSubmitMessage.Click
        'Now collect the information to write the the appropriate tables
        'EntityId, UserCampaignId, TargetListId, RecipientGo, MessageContentId, Schedule=Now, 
        Dim EntityId As Integer = CInt(Me.ddlEntity.SelectedValue)
        Dim UserCampaignId As Integer = 0
        Dim CampaignFilterId As Integer = 0
        Dim TargetListId As Integer = 0
        Dim SavedMessageGroupId As Integer = 0
        Dim MessageTypeId As Integer = 0
        Dim CampaignTypeId As Integer = 1 'Immediate 
        Dim KeyWord As String = ""

        'Determine Contact
        Dim strFirstName As String = ""
        Dim strMobileNo As String = ""
        Dim strWorkEmail As String = ""
        Dim strHomeEmail As String = ""
        Dim strTargetListName As String = ""
        Dim strSubject As String = ""
        Dim strFilterValue As String = ""
        Dim strListFilter As String = ""
        Dim strMessageContent As String = ""

        Dim bOkToProceed As Boolean = True

        If Me.rdoContactMobile.Checked Then
            'Recipient
            strMobileNo = Me.TextBoxContactMobileNumber.Text.Trim()
            strFirstName = Me.TextBoxContactMobileFirstName.Text.Trim()
            TargetListId = 0
            MessageTypeId = 1  'SMS

            'Check for errors
            If strMobileNo.Length <= 0 Or strFirstName.Length <= 0 Then
                Me.lblMessage.Text = "A Mobile Number AND a First Name is Required"
                Me.lblMessage.Visible = True
                bOkToProceed = False
            End If
        ElseIf Me.rdoContactEmail.Checked Then
            TargetListId = 0
            strHomeEmail = Me.TextBoxContactHomeEmailAddress.Text.Trim()
            strWorkEmail = Me.TextBoxContactWorkEmailAddress.Text.Trim()
            strFirstName = Me.TextBoxContactEmailFirstName.Text.Trim()
            MessageTypeId = 2  'Email

            'Check for errors
            If strFirstName.Length > 0 And (strHomeEmail.Length > 0 Or strWorkEmail.Length > 0) Then
            Else
                Me.lblMessage.Text = "A First Name AND at least one Email Address is Required"
                bOkToProceed = False
            End If
        ElseIf Me.rdoContactList.Checked Then
            strMobileNo = ""
            strWorkEmail = ""
            strHomeEmail = ""
            strFirstName = ""

            If Me.rdoTargetListAdhoc.Checked Then
                'Create a user-specified adhoc list
                strTargetListName = TextBoxTargetListName.Text
                TargetListId = 0
            ElseIf Me.rdoTargetListExisting.Checked Then
                ' No need to create a list - use an existing one
                strTargetListName = ""
                TargetListId = CInt(Me.ddlTargetList.SelectedValue)
            End If

            MessageTypeId = 0  'not needed
        End If

        If bOkToProceed Then

            'Determine CampaignFilter
            If Me.rdoFilterExisting.Checked Then
                CampaignFilterId = CInt(Me.ddlCampaignFilter.SelectedValue)
            ElseIf Me.rdoFilterNone.Checked Then
                CampaignFilterId = 0
            End If

            'get the Content and strip out dodgy characters
            strMessageContent = SanitizeString(Me.TextBoxContentMessage.Text)
            strSubject = SanitizeString(Me.TextBoxContentSubject.Text)

            SavedMessageGroupId = 0
            Me.lblMessage.Visible = False
        Else
            ' show the message 
            Me.lblMessage.Visible = True

        End If

        'Now save it to the database queue
        Try
            Dim getaassociatepersonid As New GetPersonIdFromUserId()
            Dim qsm As New LogginsLogDSTableAdapters.SPTableAdapter
            qsm.usp_SaveQuickMessage(EntityId, getaassociatepersonid.GetPersonId(CType(u.ProviderUserKey, Guid)), MessageTypeId, TargetListId, strTargetListName, KeyWord, strSubject, strFirstName, strHomeEmail, strWorkEmail, strMobileNo, CampaignFilterId, strMessageContent)

            Me.lblMessage.Text = "Message Saved Successfully"
            'Now reset the controls

            Response.Redirect("~/default.aspx")

        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Shared Function SanitizeString(ByVal OldStr As String) As String
        Dim NewStr As String = ""
        OldStr = OldStr.Trim()
        For l As Integer = 1 To Len(OldStr)
            If Asc(Mid(OldStr, l, 1)) > 31 And Asc(Mid(OldStr, l, 1)) < 127 Then
                NewStr = NewStr & Mid(OldStr, l, 1)
            End If
        Next
        Return NewStr
    End Function
    Protected Sub rdoFilterNone_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoFilterNone.CheckedChanged
        Me.MultiViewFilter.SetActiveView(Me.ViewFilter_Unfiltered)
    End Sub

    Protected Sub rdoFilterExisting_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoFilterExisting.CheckedChanged
        Me.MultiViewFilter.SetActiveView(Me.ViewFilter_Existing)
    End Sub

    Protected Sub rdoContactMobile_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoContactMobile.CheckedChanged
        Me.MultiViewContact.SetActiveView(Me.ViewContact_Mobile)
    End Sub

    Protected Sub rdoContactEmail_CheckChanged(sender As Object, e As System.EventArgs) Handles rdoContactEmail.CheckedChanged
        Me.MultiViewContact.SetActiveView(Me.ViewContact_Email)
    End Sub

    Protected Sub rdoContactList_CheckChanged(sender As Object, e As System.EventArgs) Handles rdoContactList.CheckedChanged
        Me.MultiViewContact.SetActiveView(Me.ViewContactTargetList)
    End Sub

    'Protected Sub rdoCampaignAdhoc_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoCampaignAdhoc.CheckedChanged
    '    Me.MultiViewCampaign.SetActiveView(Me.ViewCampaign_Adhoc)
    'End Sub

    'Protected Sub rdoCampaignExisting_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoCampaignExisting.CheckedChanged
    '    Me.MultiViewCampaign.SetActiveView(Me.ViewCampaign_Existing)
    'End Sub

    'Protected Sub rdoContent_Adhoc_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoContent_Adhoc.CheckedChanged
    '    Me.MultiViewContent.SetActiveView(Me.ViewContent_Adhoc)
    'End Sub

    'Protected Sub rdoContent_Predefined_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoContent_Predefined.CheckedChanged
    '    Me.MultiViewContent.SetActiveView(Me.ViewContent_Existing)
    'End Sub
    Protected Sub rdoTargetListAdhoc_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoTargetListAdhoc.CheckedChanged
        Me.MultiViewTargetList.SetActiveView(Me.ViewTargetList_Adhoc)
    End Sub

    Protected Sub rdoTargetListExisting_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoTargetListExisting.CheckedChanged
        Me.MultiViewTargetList.SetActiveView(Me.ViewTargetList_Existing)
    End Sub
End Class
